#include <iostream>
#include <iomanip>
#include <cstdio>
using namespace std;

#define SALESPERSON 4
#define PRODUCTS    5

double sales[SALESPERSON][PRODUCTS];
double column[PRODUCTS];

int main() {

    cout.setf (ios::fixed);
    cout.precision (2);

    cout << "Enter the salesperson (1 - 4). product number (1 - 5)"
         << ". and total sales\n"
         << "Enter -1 for the sales person to end input." << endl;

    char *input = new char[20];

    while (1)
    {
        input = fgets (input, 20, stdin);
        int sp, pn, rt;
        float ts;
        rt = sscanf (input, "%d %d %f", &sp, &pn, &ts);
        if (rt == 1 && sp == -1) 
            break;
        else if (rt == 3)
            if (1 <= sp && 4 >= sp && 1 <= pn && pn <= 5)
                sales[sp-1][pn-1] = (double)ts;
            else
                goto error;
        else
            goto error;

        continue;

        error:
            cout << "Wrong Format\n";
            continue;
    }

    delete[] input;

    cout << setw(6) << "" 
         << setw(10) << "product1"
         << setw(10) << "product2"
         << setw(10) << "product3"
         << setw(10) << "product4"
         << setw(10) << "product5"
         << setw(10) << "Total" << endl;

    for (int i = 0; i < SALESPERSON; ++i)
    {
        cout << setw (6) << i + 1;
        double total = 0;
        for (int j = 0; j< PRODUCTS; ++j)
        {
            cout << setw (10) << sales[i][j];
            total += sales[i][j];
            column[j] += sales[i][j];
        }
        cout << setw(10) <<  total << endl;
    }

    cout << endl << setw (6) << "Total";
    for (int k = 0; k < PRODUCTS; ++k)
        cout << setw (10) << column[k];
    cout << endl;

    return 0;
}


